#include <semaphore.h>
#include <pthread.h>
#include <stdio.h>
#include <string.h>

static sem_t sem;

static char buf[100];

static pthread_t reader_id;
static pthread_t writer_id;

void *writer(void *argv)
{
    strcpy(buf, "Hello");
    sem_post(&sem);
}

void *reader(void *argv)
{
    sem_wait(&sem);
    printf("读取缓存区: %s\n", buf);
    memset(buf, 0, 100);
}

int main(int argc, char const *argv[])
{
    sem_init(&sem, 0, 0);
    pthread_create(&reader_id, NULL, reader, NULL);
    pthread_create(&writer_id, NULL, writer, NULL);

    pthread_join(reader_id, NULL);
    pthread_join(writer_id, NULL);

    sem_destroy(&sem);

    return 0;
}
